//****************************************************************
//* Copyright (c) 2008 Ford Motor Company. All Rights Reserved.
//*
//* $$Workfile:   CurrencyAction.java  $$
//* $$Revision:   1.1  $$
//* $$Author:   JLOPE262  $$
//* $$Date:   Nov 21 2008 14:26:36  $$
//*
//*****************************************************************
package ar.com.ford.it.fullEconomics.view.action;

import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import ar.com.ford.it.fullEconomics.exception.FullEconomicsException;
import ar.com.ford.it.fullEconomics.facade.CurrencyFacade;
import ar.com.ford.it.fullEconomics.persistence.dto.Teesm01Currency;
import ar.com.ford.it.fullEconomics.view.form.CurrencyForm;
import ar.com.ford.it.fullEconomics.wslx.FullEconomicsWslxUser;
import ar.com.ford.it.webengine.facade.FacadeProvider;
import ar.com.ford.it.webengine.struts.AbstractBaseAction;
import ar.com.ford.it.webengine.struts.FordActionMapping;
import ar.com.ford.it.webengine.websession.WebSession;

public class CurrencyAction extends AbstractBaseAction {

    /**
     * Setea el mensaje de "busqueda sin resultados" cuando la busqueda no trae
     * resultados
     * 
     * @param request
     * Request donde se guarda el mensaje
     */
    private void searchWithNoResults(HttpServletRequest request) {
        ActionMessages am = new ActionMessages();
        MessageResources messageResources = (MessageResources) getResources(request);

        am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(
                "customMessage", messageResources
                        .getMessage("message.noResults")));
        saveMessages(request, am);
    }

    /**
     * Resetea el formulario y envia a la pagina inicial de Currencys
     * 
     */
    public ActionForward initPage(FordActionMapping mapping,
                                  ActionForm actionForm,
                                  HttpServletRequest request,
                                  HttpServletResponse response,
                                  WebSession webSession, ActionMessages msgs,
                                  ActionMessages warns, ActionMessages errorMsgs) {
        CurrencyForm form = (CurrencyForm) actionForm;

        form.resetForm();
        return mapping.findForward("success");
    }
    
    /**
     * Resetea los campos para el create/update
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward clearCreateFields(FordActionMapping mapping,
                                      ActionForm actionForm,
                                      HttpServletRequest request,
                                      HttpServletResponse response,
                                      WebSession webSession,
                                      ActionMessages msgs,
                                      ActionMessages warns,
                                      ActionMessages errorMsgs) {
        CurrencyForm form = (CurrencyForm) actionForm;

        form.clearCreateUpdateFields();
        return mapping.findForward("success");
    }

    /**
     * Se encarga del manejo de la busqueda de Currencys
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward findCurrencys(FordActionMapping mapping,
                                       ActionForm actionForm,
                                       HttpServletRequest request,
                                       HttpServletResponse response,
                                       WebSession webSession,
                                       ActionMessages msgs,
                                       ActionMessages warns,
                                       ActionMessages errorMsgs) {
        
        ActionMessages am = new ActionMessages();

        CurrencyForm form = (CurrencyForm) actionForm;
        CurrencyFacade facade = (CurrencyFacade) FacadeProvider.getFacade(
                request, CurrencyFacade.class);
        Collection<Teesm01Currency> res = null;
        try {
            res = facade.findCurrencys(form
                    .getCodeFind());
            if (res.isEmpty()) {
                searchWithNoResults(request);
            }
        } catch (FullEconomicsException e) {
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e.getMessageResource()));
            saveErrors(request, am);
        }
        form.setCurrencys(res);
        form.clearCreateUpdateFields();
        return mapping.findForward("success");
    }

    /**
     * Setea los campos de edicion de Currencys con los valores del Currency
     * seleccionado mediante el listado
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward editCurrency(FordActionMapping mapping,
                                      ActionForm actionForm,
                                      HttpServletRequest request,
                                      HttpServletResponse response,
                                      WebSession webSession,
                                      ActionMessages msgs,
                                      ActionMessages warns,
                                      ActionMessages errorMsgs) {
        CurrencyForm form = (CurrencyForm) actionForm;

        form.setCurrencyForEdit(form.getCurrencySelected());
        return mapping.findForward("success");
    }

    /**
     * En caso de que se haya seleccionado algun Currency del listado, entonces
     * lo actualiza, sino llama al metodo encargado de crear el Currency
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward createUpdateCurrency(FordActionMapping mapping,
                                              ActionForm actionForm,
                                              HttpServletRequest request,
                                              HttpServletResponse response,
                                              WebSession webSession,
                                              ActionMessages msgs,
                                              ActionMessages warns,
                                              ActionMessages errorMsgs) {
        
        ActionMessages am = new ActionMessages();
        
        CurrencyForm form = (CurrencyForm) actionForm;
        FullEconomicsWslxUser user = (FullEconomicsWslxUser) webSession
                .getUser();
        CurrencyFacade facade = (CurrencyFacade) FacadeProvider.getFacade(
                request, CurrencyFacade.class);
        
        try{
            //Si la moneda existe por codigo
            if (facade.findCurrencyByCode(form.getCode())== null || form.getEditBlocked().equals("false")) {
                facade.createCurrency(form.getCurrencyForCreate(), user);
            } else {
                facade.updateCurrency(form.getCurrencyForUpdate(form
                        .getCurrencySelected()), user);
            }
        }catch (FullEconomicsException e) {
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e.getMessageResource()));
            saveErrors(request, am);
        }
        return mapping.findForward("success");
    }

    /**
     * Elimina el Currency seleccionado
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @param webSession
     * @param msgs
     * @param warns
     * @param errorMsgs
     * @return
     */
    public ActionForward deleteCurrency(FordActionMapping mapping,
                                        ActionForm actionForm,
                                        HttpServletRequest request,
                                        HttpServletResponse response,
                                        WebSession webSession,
                                        ActionMessages msgs,
                                        ActionMessages warns,
                                        ActionMessages errorMsgs) {
        
        ActionMessages am = new ActionMessages();
        
        CurrencyForm form = (CurrencyForm) actionForm;
        CurrencyFacade facade = (CurrencyFacade) FacadeProvider.getFacade(
                request, CurrencyFacade.class);

        try {
            facade.deleteCurrency(form.getCurrencySelected());
        } catch (FullEconomicsException e) {
            am.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(e.getMessageResource()));
            saveErrors(request, am);
        }
        return mapping.findForward("success");
    }
}
